package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: hamo
 * @Create by: 渭北彭于晏
 * @Date: 2021/11/6 14:29
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R addShopping(@RequestBody ShoppingCart shoppingCart) {

        log.info("shopingCart {}", shoppingCart);

        shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id

        Long setmealId = shoppingCart.getSetmealId();
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();

        if (dishId != null) {
            wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            //添加到购物车的是菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else if (setmealId != null) {
            wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            //添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart shopping = shoppingCartService.getOne(wrapper);

        //判断是否已经存在
        if (shopping != null) {
            //若已存在,则在原有的数量的基础上加一
            Integer number = shopping.getNumber();
            shopping.setNumber(number + 1);
            shoppingCartService.updateById(shopping);
        } else {
            //若不存在,则设置数量默认为一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }

        return R.success("shopping");
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R list() {

        log.info("购物车..");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 清空
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R clean() {
        log.info("清空购物车..");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(wrapper);
        return R.success("好了");
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        //获取用户id
        Long userid = BaseContext.getCurrentId();
        //根据用户id获取数据信息
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userid);
        //根据前端所传id,写出对应条件
        if (dishId != null) {
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else if (setmealId != null) {
            wrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        ShoppingCart user = shoppingCartService.getOne(wrapper);

        user.setNumber(user.getNumber() - 1);//数量减一
        shoppingCartService.updateById(user);

        if (user.getNumber() == 0) {//数量减为零,清除
            shoppingCartService.removeById(user.getId());
        }


        return R.success("好了");
    }

}
